package symbols;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class ShortcutButton extends JButton {

	private String label;
	private String charCode;
	private char keyCode;
	
	/**
	 * Create a shortcutbutton
	 * @param cc character code
	 * @param kc key code
	 */
	ShortcutButton(String cc, char kc){
		super();
		setLabel(cc,kc);
		setSize(50,30);
		addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent e) {
				setBind();
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}
			
		});
	}
	
	/**
	 * Method to set the label of the button
	 * @param cc character code (string)
	 * @param kc key code
	 * @author Balazs Pete
	 */
	public void setLabel(String cc, char kc){
		charCode = cc;
		keyCode = kc;
		label = charCode+" - "+keyCode;
		setText(label);
	}
	
	/**
	 * Method to get the associated character of the button
	 * @return associated character
	 * @author Balazs Pete
	 */
	public char getChar(){
		String o = JOptionPane.showInputDialog("Press selected key");
		char ch = 0;
		if(1<o.length()){
			getChar();
		} else {
			ch = o.charAt(0);
		}
		label = charCode+" - "+o;
		setText(label);
		return ch;
	}
	
	/**
	 * Method to apply set and apply the new character and keycode from user input
	 * @author Balazs Pete
	 */
	public void setBind(){
		int oldKey = getCharCode(keyCode);
		char ch = getChar();
		int code = getCharCode(ch);
		main.MainWindow.updateShortcutManagerMap(code, charCode,oldKey);
	}
	
	/**
	 * Method to apply set and apply bind from label
	 * @author Balazs Pete
	 */
	public void setBindFromLabel(){
		int code = getCharCode(keyCode);
		main.MainWindow.updateShortcutManagerMap(code, charCode);
	}
	
	/**
	 * Method to get the associated keycode of a character
	 * @param ch character
	 * @return keycode
	 * @author Balazs Pete
	 */
	public static int getCharCode(char ch){
		switch (ch){
			case 'a': return KeyEvent.VK_A;
			case 'A': return KeyEvent.VK_A; 
			case 'b': return KeyEvent.VK_B;
			case 'B': return KeyEvent.VK_B; 
			case 'c': return KeyEvent.VK_C;
			case 'C': return KeyEvent.VK_C; 
			case 'd': return KeyEvent.VK_D;
			case 'D': return KeyEvent.VK_D; 
			case 'e': return KeyEvent.VK_E; 
			case 'E': return KeyEvent.VK_E; 
			case 'f': return KeyEvent.VK_F; 
			case 'F': return KeyEvent.VK_F; 
			case 'g': return KeyEvent.VK_G; 
			case 'G': return KeyEvent.VK_G; 
			case 'h': return KeyEvent.VK_H; 
			case 'H': return KeyEvent.VK_H; 
			case 'i': return KeyEvent.VK_I; 
			case 'I': return KeyEvent.VK_I; 
			case 'j': return KeyEvent.VK_J; 
			case 'J': return KeyEvent.VK_J; 
			case 'k': return KeyEvent.VK_K; 
			case 'K': return KeyEvent.VK_K; 
			case 'l': return KeyEvent.VK_L; 
			case 'L': return KeyEvent.VK_L; 
			case 'm': return KeyEvent.VK_M; 
			case 'M': return KeyEvent.VK_M; 
			case 'n': return KeyEvent.VK_N; 
			case 'N': return KeyEvent.VK_N; 
			case 'o': return KeyEvent.VK_O; 
			case 'O': return KeyEvent.VK_O; 
			case 'p': return KeyEvent.VK_P; 
			case 'P': return KeyEvent.VK_P; 
			case 'q': return KeyEvent.VK_Q; 
			case 'Q': return KeyEvent.VK_Q; 
			case 'r': return KeyEvent.VK_R; 
			case 'R': return KeyEvent.VK_R; 
			case 's': return KeyEvent.VK_S; 
			case 'S': return KeyEvent.VK_S; 
			case 't': return KeyEvent.VK_T; 
			case 'T': return KeyEvent.VK_T; 
			case 'u': return KeyEvent.VK_U; 
			case 'U': return KeyEvent.VK_U; 
			case 'v': return KeyEvent.VK_V; 
			case 'V': return KeyEvent.VK_V; 
			case 'w': return KeyEvent.VK_W; 
			case 'W': return KeyEvent.VK_W; 
			case 'x': return KeyEvent.VK_X; 
			case 'X': return KeyEvent.VK_X; 
			case 'y': return KeyEvent.VK_Y; 
			case 'Y': return KeyEvent.VK_Y; 
			case 'z': return KeyEvent.VK_Z; 
			case 'Z': return KeyEvent.VK_Z; 
			case '0': return KeyEvent.VK_0; 
			case '1': return KeyEvent.VK_1; 
			case '2': return KeyEvent.VK_2; 
			case '3': return KeyEvent.VK_3; 
			case '4': return KeyEvent.VK_4; 
			case '5': return KeyEvent.VK_5; 
			case '6': return KeyEvent.VK_6; 
			case '7': return KeyEvent.VK_7; 
			case '8': return KeyEvent.VK_8; 
			case '9': return KeyEvent.VK_9;
			case ' ': return KeyEvent.VK_SPACE;
			
		}
		return 0;
	}
	
}
